package cn.imu.appdevexperiment2020.Application.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import cn.imu.appdevexperiment2020.Application.api.ApiClient;
import cn.imu.appdevexperiment2020.Application.bean.user.LoginReceiveBean;
import cn.imu.appdevexperiment2020.Application.bean.user.LoginSendBean;
import cn.imu.appdevexperiment2020.Application.util.AuthUtil;
import cn.imu.appdevexperiment2020.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class LoginActivity extends AppCompatActivity {
    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;
    public static final String TAG="Login";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        accountEdit = findViewById(R.id.account);
        passwordEdit = findViewById(R.id.password2);
        TextView newUser = findViewById(R.id.newUser);
        TextView resetPwd=findViewById(R.id.resetPwd);
        TextView edit=findViewById(R.id.resetInfo);
        login = findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LoginSendBean loginSendBean = new LoginSendBean();
                loginSendBean.setUserName(accountEdit.getText().toString());
                loginSendBean.setPassword(passwordEdit.getText().toString());
                Call<LoginReceiveBean> call= ApiClient.getInstance(getApplicationContext()).getReturnData(AuthUtil.GetAuth(), loginSendBean);
                call.enqueue(new Callback<LoginReceiveBean>() {
                    @Override
                    public void onResponse(Call<LoginReceiveBean> call, Response<LoginReceiveBean> response) {
                        if(null!=response.body())
                        {
                            if(200==response.body().getCode())
                            {
                                Log.d(TAG, response.body().getMsg());
                                Toast.makeText(getApplicationContext(),"登录成功",Toast.LENGTH_SHORT).show();
                                Intent intent=new Intent(LoginActivity.this,KeyActivity.class);
                                startActivity(intent);
                            }else{
                                Toast.makeText(getApplicationContext(),response.body().getMsg(),Toast.LENGTH_SHORT).show();
                            }
                        }else
                            Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                    @Override
                    public void onFailure(Call<LoginReceiveBean> call, Throwable t) {
                        Toast.makeText(getApplicationContext(),"网络连接失败",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        newUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        resetPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,ResetPwdActivity.class);
                startActivity(intent);
            }
        });
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(LoginActivity.this,EditActivity.class);
                startActivity(intent);
            }
        });
    }

}


